package com.gateway.pay.channel.strategy.request;

import com.gateway.pay.channel.dto.BaseRequest;
import com.gateway.pay.channel.dto.ErrorCode;
import com.gateway.pay.channel.dto.Result;
import com.gateway.pay.channel.strategy.IThirdPartyStrategy;
import com.gateway.pay.channel.strategy.StrategyContext;
import com.gateway.pay.channel.strategy.factory.IThirdPartyFactory;
import com.gateway.pay.channel.strategy.factory.StrategyThirdPartyFactory;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

/**
 * 调用三方接口的步骤为：
 *   获取工厂、策略、封装策略、调用三方实际请求接口
 *   这样很麻烦，高层模块为了获取三方结果，还需要记住执行顺序
 *   这时我们可以使用门面模式来屏蔽子系统的复杂性，
 *   为高层模块提供一个调用的接口即可。
 *
 * 调用三方接口的门面
 *
 */
@Component
@Slf4j
public class RequestFacadeContext {
    private IThirdPartyFactory factory = new StrategyThirdPartyFactory();
    private StrategyContext context = new StrategyContext();


    private IThirdPartyStrategy getAdapter(String platform) {
        if (StringUtils.isBlank(platform)) {
            throw new RuntimeException("");
        }

        //latformTypeEnums strategy = PlatformTypeEnums.getPlatformType(platform);
        IThirdPartyStrategy adapter = null;//this.factory.createStrategy(strategy);

        if (adapter == null) {
            throw new RuntimeException("");
        }
        this.context.setStrategy(adapter);
        return adapter;
    }

    public Result register(BaseRequest request) {
        IThirdPartyStrategy adapter = null;//getAdapter(request.getPlatform());
        if (adapter == null) {
            throw new RuntimeException("");
         }
        if (adapter.support(null)) {//request.getPlatform()
            return this.context.doRegister(request);
        }
        return Result.failure(ErrorCode.ERROR.getCode(),"");
    }
}
